//
//  SASingeleCodeCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/31.
//

#import "SASingeleCodeCell.h"
#import "SANewGoodsModel.h"

@interface SASingeleCodeCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *colourLab;
@property (nonatomic, strong) UILabel *sizeLab;
@property (nonatomic, strong) UITextField *codeTf;
@property (nonatomic, strong) UIButton *productCodeBtn;

@end

@implementation SASingeleCodeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASkuFormModel *)model {
    _model = model;
    _colourLab.text = model.colourName;
    _sizeLab.text = model.sizeName;
    _codeTf.text = model.barcode;
}


- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 14) {
        return NO;
    }
    
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678\\9*-"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    self.model.barcode = textField.text;
}

-(void)initUI {
    [self.contentView addSubview:self.colourLab];
    [self.contentView addSubview:self.sizeLab];
    [self.contentView addSubview:self.codeTf];
    [self.contentView addSubview:self.productCodeBtn];
}

-(void)clickedProductCodeBtn {
    !self.scanBlock ?: self.scanBlock();
}

-(UILabel *)colourLab {
    if (!_colourLab) {
        _colourLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 60, 45)];
        _colourLab.font = kFont(14);
        _colourLab.textColor = [UIColor blackColor];
        _colourLab.textAlignment = NSTextAlignmentLeft;
    }
    return _colourLab;
}

-(UILabel *)sizeLab {
    if (!_sizeLab) {
        _sizeLab = [[UILabel alloc] initWithFrame:SARectMake(85, 0, 60, 45)];
        _sizeLab.font = kFont(14);
        _sizeLab.textColor = [UIColor blackColor];
        _sizeLab.textAlignment = NSTextAlignmentCenter;
    }
    return _sizeLab;
}

-(UITextField *)codeTf {
    if (!_codeTf) {
        _codeTf = [[UITextField alloc] initWithFrame:SARectMake(150, 5, 150, 35)];
        _codeTf.textAlignment = NSTextAlignmentCenter;
        _codeTf.layer.cornerRadius = kScaleNum(3);
        _codeTf.layer.masksToBounds = YES;
        _codeTf.font = kFont(14);
        _codeTf.layer.borderColor = kLightgGrayColor.CGColor;
        _codeTf.layer.borderWidth = kScaleNum(0.5);
        _codeTf.delegate = self;
        [_codeTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _codeTf;
}

-(UIButton *)productCodeBtn {
    if (!_productCodeBtn) {
        _productCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _productCodeBtn.frame = SARectMake(305, 0, 60, 40);
        [_productCodeBtn setImage:[UIImage imageNamed:@"gray_san"] forState:UIControlStateNormal];
        [_productCodeBtn addTarget:self action:@selector(clickedProductCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _productCodeBtn;
}

@end
